//引入封装请求
import request from "../../../utils/request";
const timeDate = require('../../../utils/util.js') //引入时间戳转换日期格式函数
Page({
  data: {
    leaveData: [], //历史请假数据
  },

  details: async function (e) {
    let leaveID = e.currentTarget.dataset.leaveid;

    //请假详情全部数据,，，，，，，，，，，，，
    let leaveData = [],
      appStatus, gradeName, gradName, dormName, studentName, startDate, endDate, urgName, urgTelephone, leaTypeName, caseStatus, leaCause, remDate, remStatus, isoverdue = false,
      teaName, disapproval;

    //请求根据请假Id查询请假数据
    let data = await request('/WxInterface/SelectLeaveByID', {
      leaveID
    });
    if (data.State) {
      data = data.Object;

      appStatus = data.appStatus; //请假状态
      gradeName = data.gradeName; //年级
      gradName = data.gradName; //班级
      dormName = data.dormName; //宿舍
      studentName = data.studentName; //姓名
      startDate = data.startDate.substr(6, 13); //开始时间
      endDate = data.endDate.substr(6, 13); //结束时间
      urgName = data.urgName; //紧急联系人
      urgTelephone = data.urgTelephone; //紧急联系电话
      leaTypeName = data.leaTypeName; //请假类型
      caseStatus = data.caseStatus; //家长是否知情
      leaCause = data.leaCause; //请假事由
      remDate = data.remDate; //销假时间
      remStatus = data.remStatus; //销假状态
      teaName = data.teaName; //批准人名称
      disapproval = data.disapproval; //不批准原因
      //开始与结束时间 时间戳转换日期格式 
      startDate = timeDate.formatDate(startDate, 'Y-M-D h:m:s');
      endDate = timeDate.formatDate(endDate, 'Y-M-D h:m:s');

      //销假时间 时间戳转换日期格式 
      if (remDate != null) {
        remDate=remDate.substr(6,13)
        remDate = timeDate.formatDate(remDate, 'Y-M-D h:m:s');
      }

      //请假审核状态 是否已过期
      let end = new Date(endDate).getTime();
      let newDate = new Date().getTime();
      if (end <= newDate && appStatus != 1 && appStatus != 2) {
        isoverdue = true;
      }

      leaveData = [appStatus, gradeName, gradName, dormName, studentName, startDate, endDate, urgName, urgTelephone, leaTypeName, caseStatus, leaCause, remDate, remStatus, isoverdue,teaName,disapproval]
    } else {
      wx.showToast({
        title: data.Text,
        icon: 'none',
        duration: 1800
      })
    }
    wx.setStorageSync('leaveData', JSON.stringify(leaveData)) //转成string格式
    if (leaveID > 0) {
      wx.navigateTo({ //reLaunch
        url: '/pages/personal_center/stu_leavedetails/stu_leavedetails'
      })
    } else {
      wx.showToast({
        title: "参数异常",
        icon: 'none',
        duration: 1800
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    /* 取出登录信息 字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换为数组
    let UserInfo = JSON.parse(userInfo)
    this.setData({
      /* 转完之后 存值当前页面  */
      userInfo: UserInfo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {
    let studentID = this.data.userInfo[0];
    // //发送请求获取请假信息
    let list = await request('/WxInterface/SelectLeaveData', {
      studentID
    });
    list = list.Object; //获取信息
    let startDate, leaTypeName, leaCause, start, end, appStatus, leaveID, leaveData = [],
      isoverdue = false; //isoverdue请假是否已过期

    for (var i = 0; i < list.length; i++) {
      isoverdue = false;
      //开始时间 
      start = list[i].startDate.substr(6, 13);
      startDate = timeDate.formatDate(start, 'Y-M-D h:m:s');

      leaTypeName = list[i].leaTypeName; //请假类型名称
      leaCause = list[i].leaCause; //请假事由
      leaveID = list[i].leaveID; //请假Id

      //请假审核状态
      end = Number(list[i].endDate.substr(6, 13));
      let newDate = new Date().getTime();
      appStatus = list[i].appStatus;
      if (end < newDate && appStatus != 1 && appStatus != 2) {
        isoverdue = true;
      }

      leaveData.push({
        isoverdue,
        startDate,
        appStatus,
        leaTypeName,
        leaCause,
        leaveID
      })
    }
    this.setData({
      leaveData: leaveData,
    })

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})